package com.lineage.server.clientpackets;

import com.lineage.config.ConfigOther;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1PcInventory;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_SelectList extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_SelectList.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (!pc.isGhost() && 
        !pc.isDead() && 
        !pc.isTeleport() && 
        !pc.isPrivateShop()) {
        int itemObjectId = readD();
        int npcObjectId = readD();
        if (npcObjectId != 0) {
          L1Object obj = World.get().findObject(npcObjectId);
          if (obj != null && obj instanceof L1NpcInstance) {
            L1NpcInstance npc = (L1NpcInstance)obj;
            int difflocx = Math.abs(pc.getX() - npc.getX());
            int difflocy = Math.abs(pc.getY() - npc.getY());
            if (difflocx > 5 || difflocy > 5)
              return; 
          } 
          L1PcInventory pcInventory = pc.getInventory();
          L1ItemInstance item = pcInventory.getItem(itemObjectId);
          int cost = item.get_durability() * 200;
          if (!pc.getInventory().consumeItem(40308, cost)) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(189));
            return;
          } 
          item.set_durability(0);
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(464, item.getLogName()));
          pcInventory.updateItem(item, 1);
          return;
        } 
        if (ConfigOther.petcountcha) {
          pc.petReceive(itemObjectId);
          return;
        } 
        pc.petReceive1(itemObjectId);
        return;
      } 
      return;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
